package com.common.controller;

import cn.hutool.json.JSONUtil;
import com.common.authentication.Oauth2ModeAuthentication;
import com.common.enums.ApiResult;
import com.common.model.BusinessUserModel;
import com.common.service.BusinessUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Date: 2025/8/24 14:08
 */

@RestController
@RequestMapping("/v1/business-user")
public class BusinessUserController {

    @Autowired
    private BusinessUserService businessUserService;

    @GetMapping("/current/info")
    public ApiResult currentInfo() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication instanceof Oauth2ModeAuthentication oauth2ModeAuthentication){
            Object businessId = oauth2ModeAuthentication.getBusinessId();
            BusinessUserModel currentUserInfoModel = businessUserService.getById((String) businessId);
            if (!ObjectUtils.isEmpty(currentUserInfoModel)){
                return ApiResult.success(currentUserInfoModel);
            }
        }
        return ApiResult.success(JSONUtil.createObj());
    }
}
